//
//  TribeDetailHeaderView.m
//  yigou
//
//  Created by SunChao on 17/2/8.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "TribeDetailHeaderView.h"
#import "LaBaView.h"
#import "TribeInfo.h"
#import "HisHomePageViewController.h"
#define TopViewHeight  281
#define BottomViewHeight  195+30
#define RandColor [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0f]
@implementation TribeDetailHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _dataSource = [NSMutableDictionary new];
        
        [self initView];
    }
    return self;
}
- (void)refreshHeaderViewData:(NSMutableDictionary *)dataSource
{
    NSDictionary *dic = [dataSource objectForKey:@"tribeInfo"];
    TribeInfo *info = [[TribeInfo alloc]initWithDictionary:dic error:nil];
    [_bgImgView sd_setImageWithURL:[NSURL URLWithString:info.tribe_bg_img] placeholderImage:[UIImage imageNamed:@"placeholder.png"] width:SCREEN_WIDTH height:_bgImgView.frame.size.height];

    [_tribeImgView sd_setImageWithURL:[NSURL URLWithString:info.tribe_logo] placeholderImage:[UIImage imageNamed:@"tabmyp"]];
    
    _fabuLabel.text = [NSString stringWithFormat:@"发布数：%@",info.tribe_publish];
    _fabuLabel.textColor = [UIColor whiteColor];
    _renqiLabel.text = [NSString stringWithFormat:@"人气：%@",info.tribe_popularity];
    _renqiLabel.textColor = [UIColor whiteColor];
    
    [_qiandaoBtn setBackgroundColor:QINGColor];
    [_qiandaoBtn setTintColor:[UIColor whiteColor]];
    [_qiandaoBtn setTitle:@"签到" forState:UIControlStateNormal];
    [_qiandaoBtn setTitle:@"已签到" forState:UIControlStateSelected];
    if (info.today_is_sign == YES) {
        _qiandaoBtn.selected = YES;
    }
    [_zhaoqiuzhangBtn setBackgroundColor:QINGColor];
    [_zhaoqiuzhangBtn setTintColor:[UIColor whiteColor]];
    [_zhaoqiuzhangBtn setTitle:@"找酋长" forState:UIControlStateNormal];
    [_zhaoqiuzhangBtn setTitle:@"找酋长" forState:UIControlStateSelected];
    
    [_qiandaoBtn addTarget:self action:@selector(QianDaoAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_zhaoqiuzhangBtn addTarget:self action:@selector(ZhaoQiuZhangAction:) forControlEvents:UIControlEventTouchUpInside];
    if (info.today_is_sign == YES) {
        _qiandaoBtn.selected = YES;
    }
    [_zhaoqiuzhangBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    _tribeNameLabel.text = info.tribe_name;
    _tribeTitleLabel.text = info.tribe_desc;
    
    
    CGRect frame = CGRectMake(15, BottomViewHeight - 100, SCREEN_WIDTH-30, 90);
    
    NSMutableArray *array = [NSMutableArray new];
    NSArray *array1 = [self.dataSource objectForKey:@"topData1"];
    NSInteger num1 = array1.count;
    NSArray *array2 = [self.dataSource objectForKey:@"topData2"];
    NSInteger num2 = array2.count;
    NSArray *array3 = [self.dataSource objectForKey:@"topData3"];
    NSInteger num3 = array3.count;
    
   NSInteger result= num1>num2?num1:num2;
    
    result= result>num3?result:num3;
    
    if (result != 0) {
        for (int index = 0; index<result; index++) {
            UIView *view = [[UIView alloc]initWithFrame:frame];
            for (int i = 0; i<3; i++) {
                
                LaBaView *tmpView = [[LaBaView alloc]initWithFrame:CGRectMake(0, 30*i, SCREEN_WIDTH, 30)];
                
                switch (i) {
                    case 0:
                        tmpView.label.text = @"内容";
                        if (array1.count>index&&array1 != nil) {
                            NSDictionary *tmpDic = [array1 objectAtIndex:index];
                            tmpView.detailLabel.text = [NSString stringWithFormat:@"%@",[tmpDic objectForKey:@"desc"]];
                        }else{
                            tmpView.detailLabel.text = @"暂无内容";
                        }
                        
                        break;
                    case 1:
                        tmpView.label.text = @"动态";
                        if (array2.count>index&&array2 != nil) {
                            NSDictionary *tmpDic = [array2 objectAtIndex:index];
                            tmpView.detailLabel.text = [NSString stringWithFormat:@"%@",[tmpDic objectForKey:@"desc"]];
                        }else{
                            tmpView.detailLabel.text = @"暂无内容";
                        }

                        break;
                    case 2:
                        tmpView.label.text = @"其他";
                        if (array3.count>index&&array3 != nil) {
                            NSDictionary *tmpDic = [array3 objectAtIndex:index];
                            tmpView.detailLabel.text = [NSString stringWithFormat:@"%@",[tmpDic objectForKey:@"desc"]];
                        }else{
                            tmpView.detailLabel.text = @"暂无内容";
                        }
                        break;
                        
                    default:
                        break;
                }
                
                [view addSubview:tmpView];
                
            }
            [array addObject:view];
        }
        HADirect *bottomlabaView = [HADirect direcWithtFrame:frame ViewArr:array AndClickBlock:^(NSInteger index) {
            
        }];
        
        [_bottomView addSubview:bottomlabaView];
    }
}

-(void) initView
{
    float x = 0;
    float y = 0;
    float width = SCREEN_WIDTH;
    float height = TopViewHeight;
    UIView *topView= [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [self addSubview:topView];
    
    _bgImgView = [[UIImageView alloc]initWithFrame:topView.frame];
    [topView addSubview:_bgImgView];
     y = CGRectGetMaxY(topView.frame);
    if (_fabuLabel == nil) {
        
        _fabuLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, y-30, 150, 20)];
        [topView addSubview:_fabuLabel];
    }
    
    y = CGRectGetMinY(_fabuLabel.frame);
    if (_renqiLabel == nil) {
        _renqiLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, y-30, 150, 20)];
        [topView addSubview:_renqiLabel];
    }
    
    if (_zhaoqiuzhangBtn == nil) {
        _zhaoqiuzhangBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 15 -76, TopViewHeight-40, 76, 25)];
        _zhaoqiuzhangBtn.layer.cornerRadius = 8;
        _zhaoqiuzhangBtn.layer.masksToBounds = YES;
        [_zhaoqiuzhangBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
        [_zhaoqiuzhangBtn setImage:[UIImage imageNamed:@"签到"] forState:UIControlStateNormal];
        _zhaoqiuzhangBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [topView addSubview:_zhaoqiuzhangBtn];
    }
    y = CGRectGetMinY(_zhaoqiuzhangBtn.frame);
    if (_qiandaoBtn == nil) {
        _qiandaoBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 15 -76, y - 35, 76, 25)];
        _qiandaoBtn.layer.cornerRadius = 8;
        _qiandaoBtn.layer.masksToBounds = YES;
        [_qiandaoBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
        [_qiandaoBtn setImage:[UIImage imageNamed:@"签到"] forState:UIControlStateNormal];
        
        _qiandaoBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_qiandaoBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        
        [topView addSubview:_qiandaoBtn];
    }
    
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, height, width, BottomViewHeight)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bottomView];
    
    if (_tribeNameLabel == nil) {
        _tribeNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 43, SCREEN_WIDTH, 20)];
        _tribeNameLabel.textAlignment = NSTextAlignmentCenter;
        [_bottomView addSubview:_tribeNameLabel];
    }
    
    y = CGRectGetMaxY(_tribeNameLabel.frame)+10;
    if (_tribeTitleLabel == nil) {
        _tribeTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, y, SCREEN_WIDTH-50, 20)];
        _tribeTitleLabel.textAlignment = NSTextAlignmentCenter;
        _tribeTitleLabel.font = [UIFont systemFontOfSize:13];
        _tribeTitleLabel.textColor = [UIColor grayColor];
        [_bottomView addSubview:_tribeTitleLabel];
       
    }

    _tribeImgView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-33, 281-33, 66, 66)];
//    [self addSubview:midView];
//    _tribeImgView = [[UIImageView alloc]initWithFrame:midView.frame];
//    _tribeImgView.backgroundColor = [UIColor grayColor];
    [self addSubview:_tribeImgView];
    
}

- (void)QianDaoAction:(UIButton *)button{
    NSLog(@"点击签到");
    if ((button.selected == YES)) {
        [ViewFactory showInfoMsg:@"已签到"];
        return;
    }
    [self loadNetworkData];
}
- (void)loadNetworkData {
    
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *url = [NSString stringWithFormat:@"%@/hytribe/signTribe?tribe_id=%@", APIYIGOU,_tribeID];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        [ViewFactory showSuccess:@"签到成功"];
        
        self.qiandaoBtn.selected = YES;
        
    } error:^(YTKBaseRequest *request) {
        
        [ViewFactory showErrorStrMsg:@"签到失败"];
        
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        
    }];
}
- (void)ZhaoQiuZhangAction:(UIButton *)button{
    NSLog(@"点击找酋长");
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *url = [NSString stringWithFormat:@"%@/hytribe/getUserInfoByTribeId?tribe_id=%@", APIYIGOU,_tribeID];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        DYLog(@"dict:%@", dict.description);
        NSString *theuid = [[dict objectForKey:@"data"] objectForKey:@"user_id"];
        EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:theuid conversationType:EMConversationTypeChat];
        if (self.vc)
            [self.vc.navigationController pushViewController:chatController animated:YES];
    } error:^(YTKBaseRequest *request) {
       
        [ViewFactory showErrorStrMsg:@"请求失败"];
        
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        
    }];
    
}

@end
